package com.sunhao.blog.model.vo;

import com.sunhao.blog.enums.ResultEnum;
import lombok.Data;

@Data
public class ResultVO<T> {
    private int code;
    private String message;
    private T data;


    /*
     * 响应类自定义常用快捷方法
     * */
    public static <T> ResultVO<T> success() {
        //操作成功，直接调用无参构造
        return new ResultVO<>();
    }

    public static <T> ResultVO<T> success(T data) {
        //操作成功并返回数据
        return new ResultVO<>(data);
    }

    public static <T> ResultVO<T> fail() {
        //操作失败
        return new ResultVO<T>(ResultEnum.fail);
    }

    public static <T> ResultVO<T> fail(String message) {
        //操作失败，自定义失败message
        return new ResultVO<T>(ResultEnum.fail.getCode(), message);
    }


    /*
     * 响应类构造方法
     * */

    //传入响应码和方法
    public ResultVO(int code, String message) {
        this.code = code;
        this.message = message;
    }

    //传入枚举类
    public ResultVO(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    //不传参时，默认为操作成功
    public ResultVO() {
        this.code = ResultEnum.OK.getCode();
        this.message = ResultEnum.OK.getMessage();
    }

    //需要传入数据时，默认200 操作成功,并返回数据
    public ResultVO(T data) {
        this.code = ResultEnum.OK.getCode();
        this.message = ResultEnum.OK.getMessage();
        this.data = data;
    }

    //操作成功，返回数据，自定义message
    public ResultVO(T data, String message) {
        this.code = ResultEnum.OK.getCode();
        this.message = message;
        this.data = data;
    }




}
